#!/usr/bin/python

import pygame
import sys

class isoTest:

    def __init__(self):
        self.m_TileWidth = 64
        self.m_TileHeight = 32
       

    def loadImage(self):
        self.m_img = pygame.image.load("IsoMetricCave.png")
        self.m_img = self.m_img.convert_alpha()
        self.m_imgRect = pygame.Rect((0,32),(64,32))
        
    def drawTile(self, screen, xOffset, yOffset, tileX, tileY):
        screenX=tileX*(self.m_TileWidth / 2) - tileY * (self.m_TileWidth/2) - xOffset
        screenY=tileX * (self.m_TileHeight / 2) + tileY * (self.m_TileHeight/2) - yOffset

        screen.blit(self.m_img,(screenX,screenY),self.m_imgRect)
        
    def draw(self,screen,x,y):
        (w,h) = screen.get_size();
        startTileX= (x * self.m_TileHeight + y * self.m_TileWidth) / (self.m_TileWidth * self.m_TileHeight)-2
        startTileY= (y * self.m_TileWidth - x * self.m_TileHeight) / (self.m_TileWidth * self.m_TileHeight)

        tileW = (w/self.m_TileWidth)+2;
        tileH = (h/self.m_TileHeight)+3;

        colStartTileY=startTileY
        colStartTileX=startTileX
        for tileRow in range(0, tileH):
            tileY=colStartTileY
            tileX=colStartTileX
            for tileCol in range(0, tileW):
                # Draw tile X, Y
                self.drawTile(screen, x, y, tileX, tileY)
                tileX +=1
                # Draw Tile X, Y
                self.drawTile(screen, x, y, tileX, tileY)
                tileY -=1
            colStartTileY += 1
            colStartTileX += 1



    def setScreen(self,screenSurface):
        self.m_surface = screenSurface




if __name__ == "__main__":

    pygame.init()
    screen = pygame.display.set_mode((800,600),0,32)
    
    pygame.display.update()
    clock = pygame.time.Clock()
    iso = isoTest()
    iso.loadImage()


    x = 0
    y = 0
    frameCount=0
    try:
        while(True):
            clock.tick(40)
            screen.fill((0,0,0))

            pygame.event.pump()
            
            keyStates = pygame.key.get_pressed()


            if keyStates[pygame.K_UP] == True:
                y -= 1
               
            if keyStates[pygame.K_DOWN] == True:
                y += 1
               
            if keyStates[pygame.K_RIGHT] == True:
                x += 1
               
            if keyStates[pygame.K_LEFT] == True:
                x -= 1
        
            iso.draw(screen,x,y)

            consoleFont = pygame.font.Font(None,18)
            fpsString = "FPS %d" % (clock.get_fps())
            rTxt = consoleFont.render((fpsString),1,(255,255,255))
            rTxt.set_alpha(10)
            screen.blit(rTxt,(0,0)) 
            pygame.display.flip()



         
    except KeyboardInterrupt:
        print "Quiting"
        sys.exit(1)

    








